package cn.zmit.mydemo10;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.IntegerRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button mBtnLogin;
    private EditText mEtPhone;
    private EditText mEtPassword;
    private TextView mTvForgetpwd;
    private TextView mTvRegister;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mEtPassword = (EditText) findViewById(R.id.et_passwprd);
        mTvForgetpwd = (TextView) findViewById(R.id.tv_forgetpwd);
        mTvForgetpwd.setOnClickListener(this);
        mTvRegister = (TextView) findViewById(R.id.tv_register);
        mTvRegister.setOnClickListener(this);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_forgetpwd:
                toFindPwd();
                break;
            case R.id.tv_register:
                toRegister();
                break;
            case R.id.btn_login:
                toLogin();
                break;
        }

    }

    /**
     * 登录
     */
    private void toLogin() {
        Log.i("Main", "toLogin()");
        String phone = mEtPhone.getText().toString();
        String password = mEtPassword.getText().toString();
        Log.i("Main", "phone:" + phone + "   password:" + password);
        if (phone.isEmpty() || password.isEmpty()) {
            Toast.makeText(MainActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            Log.i("Main", "用户名和密码不能为空");
        } else {
            postLogin(phone, password);
        }
    }

    /**
     * 找回密码
     */
    private void toFindPwd() {
        Log.i("Main", "toFindPwd()");
        Intent intent = new Intent(MainActivity.this, FindPwdActivity.class);
        startActivity(intent);

    }

    /**
     * 注册
     */
    private void toRegister() {
        Log.i("Main", "toRegister()");
        Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
        startActivity(intent);

    }

    private void postLogin(String phone, String password) {
        Log.i("Main", "postLogin()");
        String url = "http://www.b.iffresh.com/webservice/auth/login";
//        Map<String, String> map = new HashMap();
//        map.put("phone", phone);
//        map.put("pwd", password);

        OkHttpUtils
                .post()
                .url(url)
//                .params(map)
                .addParams("phone", phone)
                .addParams("pwd", password)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("Main", "onError:" + e);
                        Toast.makeText(MainActivity.this, "Error:" + e, Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        String errorCode = null;
                        Log.i("Main", "onResponse:" + response);
                        try {
                            JSONObject mJsonObject = new JSONObject(response);
                            errorCode = mJsonObject.getString("errorCode");
                            Log.i("Main", "errorCode:" + errorCode);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (errorCode.equals("0")) {
                            Intent intent = new Intent(MainActivity.this, FruitprofusionActivity.class);
                            startActivity(intent);
                            Log.i("Main", "登陆成功");
                        } else {
                            Toast.makeText(MainActivity.this, "Error:" + response, Toast.LENGTH_SHORT).show();
                            Log.i("Main", "登陆失败：" + response);
                        }
                    }
                });
    }

}
